<?php
/**
 * Create by 11li
 * Date 2020/1/18
 * Time 14:45
 *
 */

namespace app\api\controller\v3;


use app\common\controller\Api;
use app\common\model\AgentShop;
use app\common\model\ShipOrder;
use app\common\service\AgentService;
use app\common\service\ShipService;
use app\admin\model\User as UserModel;
use app\common\service\StockService;
use think\Db;

class Agent extends Api {
	/**
	 * 获取发货下级 (总部发给广东火爆街，提货点; 广东火爆街发给场景店，重庆提货点发货下级场景店）
	 */
	public function getSon() {
		$userId = $this->auth->id;
		$level = $this->auth->level;
		
		if($level == UserModel::LEVEL_HEADQUARTERS) {
			$where = 'a.status = '.AgentShop::STATUS_Y;
			$where.=' and ((a.area = '.UserModel::AREA_SZ.' and a.role = '.UserModel::LEVEL_CENTER;
			$where.= ') or (a.role = '.UserModel::LEVEL_WAREHOUSE.'))';
		} else {
			$agentId = ShipService::getAgentId($userId);
			
			if($agentId === 0) {
				$where = 'a.status = '.AgentShop::STATUS_Y;
				$where.=' and ((a.area = '.UserModel::AREA_SZ.' and a.role = '.UserModel::LEVEL_CENTER;
				$where.= ') or (a.role = '.UserModel::LEVEL_WAREHOUSE.'))';
			} else {
				!$agentId && $this->error('不是代理');
				$agent = AgentShop::get($agentId);
				
				$where = 'a.status = '.AgentShop::STATUS_Y.' and a.area = '.$agent['area']. ' and a.role = '.UserModel::LEVEL_SHOP;
				if($agent['role'] == UserModel::LEVEL_CENTER && $agent['area'] == UserModel::AREA_SZ) {
					$where .= ' and a.center_id = '.$agent['id'];
				} else if($agent['role'] == UserModel::LEVEL_WAREHOUSE) {
					$where .= ' and a.warehouse_id = '.$agent['id'];
				} else {
					$this->error('没有发货权限');
				}
			}
			
			
		}
		$model = Db::name('agentShop');
		$keyword = $this->request->get('keyword');
		if($keyword) {
			$model->where(['a.name' => ['LIKE', '%'.$keyword.'%']]);
		}
		$agents = $model->alias('a')
			->where($where)
			->join('user u', 'a.user_id = u.id', 'left')
			->join('area p', 'a.provice_id = p.id', 'left')
			->join('area c', 'a.city_id = c.id', 'left')
			->join('area aa', 'a.area_id = aa.id', 'left')
			->join('shipOrder o', 'a.id = o.sign_agent_id and o.status = 0', 'left')
			->field('a.role, a.image, a.id, a.name, a.address, u.area, u.reward_number, a.mobile, p.name as province_name, c.name as city_name, aa.name as area_name, o.id as order_id')
			->group('a.id')
			->order(['role' => 'desc', 'name' => 'asc'])
			->select();
		
		if($agents) {
			$roleList = UserModel::$levelList;
			foreach ($agents as &$agent) {
				$agent['address'] = $agent['province_name'].$agent['city_name'].$agent['area_name'].$agent['address'];
				if($level == UserModel::LEVEL_HEADQUARTERS) {
//					$agent['name'] = '【'.$roleList[$agent['role']].'】'.$agent['name'];
					$agent['sign_number'] = Db::name('shipOrder')->where(['sign_agent_id' => $agent['id'], 'status' => ['neq', ShipOrder::STATUS_SUCCESS]])->count();
				}
			}
		}
		
		$this->success('查询成功', $agents);
	}
	
	
	
	/**
	 * 关闭代理
	 */
	public function close() {
		$agentId = $this->request->get('agent_id');
		!$agentId && $this->error('参数错误');
		
		$userId = $this->auth->id;
		$level = $this->auth->level;
		
		$service = new AgentService();
		$res = $service->closeAgent($userId, $level, $agentId);
		if($res) {
			$this->success('操作成功');
		} else {
			$this->error($service->getError());
		}
	}
	
	/**
	 * 获取代理库存
	 */
	public function getAgentStock($agent_id) {
		$list = Db::name('agentStock')->alias('s')
			->join('goods g', 's.goods_id = g.id', 'left')
			->join('goodsSpec gs', 's.spec_id = gs.id', 'left')
			->where(['s.agent_shop_id' => $agent_id])
			->field('s.id, s.number, g.image, g.name as goods_name, gs.name as spec_name')
			->select();
		$this->success('查询成功', $list);
	}
	
	/**
	 * 编辑下级库存
	 */
	public function changeSonStock() {
		$data = $this->request->post();
		if(empty($data['agent_id']) || empty($data['stock_list'])) {
			$this->error('参数错误');
		}
		
		$obj = new StockService();
		$res = $obj->changeSonStockNumber($this->auth->id, $data['agent_id'], $data['stock_list']);
		if($res) {
			$this->success('更改成功');
		} else {
			$this->error($obj->getError());
		}
	}
}